package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;


public class MyAspect {



    @Autowired
    private  JedisPool jedisPool;

    //前置增强方法
    public void before1(JoinPoint joinPoint) {

        //获取当前切点的方法名
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName);

        if("findallitem".equals(methodName)|| "FindAll".equals(methodName )){
            return;
        }

        Jedis jedis = jedisPool.getResource();
        //将score取出
        Double zscore = jedis.zscore("interfaceCount", "item_"+methodName);

        double v = (zscore == null) ? 1.0 : (zscore + 1.0D);

        jedis.zadd("interfaceCount",v+System.currentTimeMillis()/10000000000000D,"item_"+methodName);

/*        Set<String> mysortedset = jedisPool.getResource().zrange("interfaceCount", 0, -1);
        if(mysortedset.size()!=0){
            for (String s : mysortedset) {
                System.out.println(s+"-"+jedis.zscore("interfaceCount",s));
            }
        }*/
    }

    //前置增强方法
    public void before2(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName);

        if("findallitem".equals(methodName) ||
                "setcheckgroupandcheckitem".equals(methodName)||
                "copyback".equals(methodName)||
                "copybackids".equals(methodName)){
            return;
        }

        Jedis jedis = jedisPool.getResource();

        Double zscore = jedis.zscore("interfaceCount", "group_"+methodName);
        double v = (zscore == null) ? 1.0 : (zscore + 1.0);
        jedis.zadd("interfaceCount",v+System.currentTimeMillis()/10000000000000D,"group_"+methodName);

        Set<String> mysortedset = jedisPool.getResource().zrange("interfaceCount", 0, -1);
        if(mysortedset.size()!=0){
            for (String s : mysortedset) {
                System.out.println(s+"-"+jedis.zscore("interfaceCount","group_"+s));
            }
        }
    }
}
